package com.hou.serviceuser.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.HashMap;

import static java.util.Arrays.asList;
@Api(value = "用户接口")
@RestController
public class UserController {


    @Autowired
    private EurekaClient eurekaClient;


    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    @LoadBalanced
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient; //负载均衡客户端

    @PostConstruct
    public void init(){
        System.out.println("-------------初始化");

    }

    // API注释,方便查看
    @ApiOperation(value = "获取用户姓名",notes = "查询test")
    @GetMapping("/name")
    public String getUserName(){
        return "侯征";
    }

    /**
     * 获取order服务中的数据
     * @return
     */
    @ApiResponses(
            {@ApiResponse(code = 404,message = "方法找不到"),@ApiResponse(code=500,message = "接口报错")
            })
    @ApiOperation(value = "根据id获取用户订单",notes = "查询order")
    @ApiImplicitParam(name = "id",value = "订单id")
    @GetMapping("/order")
    public String test(@ApiParam(value = "订单号",required = true) String id){
        //使用 discoveryClient
        //InstanceInfo nextServerFromEureka = discoveryClient.getNextServerFromEureka("service-order", false);
        //根据注册的服务名称获取url,使用restTemplate调用服务
        InstanceInfo serverFromEureka = eurekaClient.getNextServerFromEureka("service-order", false);
        // 使用服务名调用接口,可以做负载均衡,不会限制于服务的ip不同
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(asList(MediaType.APPLICATION_JSON));
        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
        return restTemplate.getForObject("http://service-order/order/name",String.class);
        //return restTemplate.getForObject(serverFromEureka.getHomePageUrl()+"/order",String.class);
    }

    //测试负载均衡方式
    @GetMapping("/test")
    public String test01(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("service-order");//通过负载均衡查找对应服务实例
        System.out.println(serviceInstance.getServiceId()+","+serviceInstance.getHost()+","+serviceInstance.getPort());
        return serviceInstance.getPort()+"";
    }

    //使用restTemplate
    @GetMapping("/rest")
    public String test02(String param1){
        /**
         * GET请求:
         */
        //使用数组
        restTemplate.
                getForObject("http://server-user/{param1}",String.class,param1,"param2","param3");
        //拼接
        restTemplate.
                getForObject("http://server-user?param1="+param1,String.class);
        //使用map
        restTemplate.
                getForObject("http://server-user/{param1}",String.class,new HashMap<>().put("param1","param1"));
        //Entity:可以获取返回码,请求头等信息,返回数据在body里,同样有三个重载方法
        ResponseEntity<String> forEntity = restTemplate.
                getForEntity("http://server-user", String.class,"param1");
        if(forEntity.getStatusCodeValue()==200){
            System.out.println(forEntity.getBody());
        }
        /**
         * POST请求
         */
        restTemplate.postForObject("http://server-user",new Object(),String.class);



        return null;
    }

}
